package com.family.pedigree.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.family.pedigree.domain.FamilyArticle;
import com.family.pedigree.mapper.FamilyArticleMapper;
import com.family.pedigree.service.IFamilyArticleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author forever
 * Date 2024/11/17 18:17
 */
@Service
@RequiredArgsConstructor
public class FamilyArticleServiceImpl extends ServiceImpl<FamilyArticleMapper, FamilyArticle> implements IFamilyArticleService {

    private final FamilyArticleMapper familyArticleMapper;

    @Override
    public FamilyArticle getNotice() {
        FamilyArticle notice = baseMapper.selectNotice();

        return baseMapper.selectOne(Wrappers.<FamilyArticle>lambdaQuery()
                .eq(FamilyArticle::getCategory, "1")
                .eq(FamilyArticle::getStatus, "1")
                .orderByDesc(FamilyArticle::getPublishDate)
                .last("LIMIT 1"));
    }

    @Override
    public List<FamilyArticle> getTopFive() {
        return list(Wrappers.<FamilyArticle>lambdaQuery().eq(FamilyArticle::getCategory, "0")
                .eq(FamilyArticle::getStatus, "1")
                .orderByDesc(FamilyArticle::getGrade)
                .orderByDesc(FamilyArticle::getPublishDate)
                .last("LIMIT 5"));
    }

    @Override
    public List<?> getDraftList(FamilyArticle article) {
        return lambdaQuery().
                like(StringUtils.isNotBlank(article.getTitle()), FamilyArticle::getTitle, article.getTitle())
                .eq(article.getGrade() != null, FamilyArticle::getGrade, article.getGrade())
                .like(StringUtils.isNotBlank(article.getAuthor()), FamilyArticle::getAuthor, article.getAuthor())
                .eq(StringUtils.isNotBlank(article.getCategory()), FamilyArticle::getCategory, article.getCategory())
                .eq(StringUtils.isNotBlank(article.getStatus()), FamilyArticle::getStatus, 0)
                .orderByDesc(FamilyArticle::getCreateTime).list();
    }

    @Override
    public List<?> getDownList(FamilyArticle article) {
        return lambdaQuery().
                like(StringUtils.isNotBlank(article.getTitle()), FamilyArticle::getTitle, article.getTitle())
                .eq(article.getGrade() != null, FamilyArticle::getGrade, article.getGrade())
                .like(StringUtils.isNotBlank(article.getAuthor()), FamilyArticle::getAuthor, article.getAuthor())
                .eq(StringUtils.isNotBlank(article.getCategory()), FamilyArticle::getCategory, article.getCategory())
                .eq(StringUtils.isNotBlank(article.getStatus()), FamilyArticle::getStatus, 3)
                .orderByDesc(FamilyArticle::getCreateTime).list();
    }

    @Override
    public void add(FamilyArticle article) {
        //todo:人需要修改
        save(article
                .setCreateBy("admin")
                .setStatus("0").setCreateTime(LocalDateTime.now())
                .setUpdateBy("admin")
                .setUpdateTime(LocalDateTime.now()));
    }

    @Override
    public List<?> getList(FamilyArticle article) {
        return lambdaQuery().
                like(StringUtils.isNotBlank(article.getTitle()), FamilyArticle::getTitle, article.getTitle())
                .eq(article.getGrade() != null, FamilyArticle::getGrade, article.getGrade())
                .like(StringUtils.isNotBlank(article.getAuthor()), FamilyArticle::getAuthor, article.getAuthor())
                .eq(StringUtils.isNotBlank(article.getCategory()), FamilyArticle::getCategory, article.getCategory())
                 .eq(StringUtils.isNotBlank(article.getStatus()), FamilyArticle::getStatus, 3)
                .orderByDesc(FamilyArticle::getCreateTime).list();
    }

    @Override
    public void edit(FamilyArticle article) {
        if (article.getArticleId() == null) return;
        if (article.getStatus().equals("1")) throw new RuntimeException("文章已发布不能编辑");

        updateById(article.setUpdateTime(LocalDateTime.now()));

    }

    @Override
    public void upOrDown(FamilyArticle article) {
        String status = article.getStatus();
        //下架
        if (status.equals("0")) {
            updateById(article.setUpdateTime(LocalDateTime.now()));
        }
        //上架
        if (status.equals("1")) {
            updateById(article.setPublishDate(LocalDateTime.now()).setUpdateTime(LocalDateTime.now()));
        }
    }

    @Override
    public void articleCase(Long articleId) {
        FamilyArticle article = getById(articleId);
        if (Objects.nonNull(article)) {

            updateById(article.setUpdateTime(LocalDateTime.now()).setStatus("3"));
        }
    }
}
